package com.nebula.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nebula.common.Const;
import com.nebula.common.ServerResponse;
import com.nebula.pojo.Folders;
import com.nebula.pojo.User;
import com.nebula.service.IFolderService;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("folder")
public class FolderController {
	
	@Autowired
	private IFolderService folderService;
	
	@RequestMapping(value = "save",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse folderSave(HttpSession session,@RequestBody Folders folders){
		return folderService.saveOrUpdateFolder(folders);
	}
	
	@RequestMapping("/upload")
	public String gotoUpload(HttpServletRequest request){
		int folderId = Integer.parseInt(request.getParameter("folderId"));
		Folders folders = folderService.getFolderNameByFolderId(folderId);
		request.setAttribute("folderName", folders.getFolderName());
		request.setAttribute("folderId", folderId);
		return "upload";
	}
	
	@RequestMapping(value = "/plDel" , method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> plDelete(HttpSession session,String groupId){
		User user = (User)session.getAttribute(Const.USERINFO);
		return folderService.plDel(groupId,user);
	}
	
	@RequestMapping(value = "/copyTo" , method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> copyTo(@RequestParam(value = "folderid") Integer folderid,
			@RequestParam(value = "valArr") String valArr,HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		return folderService.copyTo(folderid, valArr,user);
	}
	
	@RequestMapping(value = "/moveTo" , method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> moveTo(HttpSession session,
			@RequestParam(value = "folderid") Integer folderid,
			@RequestParam(value = "valArr") String valArr){
		User user = (User)session.getAttribute(Const.USERINFO);
		return folderService.moveTo(folderid, valArr,user);
	}
	
	@RequestMapping(value = "/saveTo" , method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse<String> saveTo(HttpSession session,
			@RequestParam(value = "folderid") Integer folderid,
			@RequestParam(value = "valArr") String valArr){
		User user = (User)session.getAttribute(Const.USERINFO);
		return folderService.saveTo(folderid, valArr, user);
	}
	
	@RequestMapping("/get_directory")
	@ResponseBody
	public JSONArray getChildrenParallelFolder(@RequestParam(value = "folderid" ,defaultValue = "0") Integer folderId
			,HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		return folderService.getChildrenParallelFolder(folderId,user);
	}
	
	@RequestMapping("/get_deep_folder")
	@ResponseBody
	public ServerResponse getFoldersAndDeepChildrenFolders(@RequestParam(value = "folderid" ,defaultValue = "0") Integer folderId
			,HttpSession session){
		User user = (User)session.getAttribute(Const.USERINFO);
		return folderService.selectFoldersAndChildrenById(folderId,user);
	}
	
	@RequestMapping(value = "/get-parentid",method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse getParentId(@RequestParam("folderId") int folderid){
		return folderService.getParentId(folderid);
	}
}
